package com.cqcomponents.tags;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.api.scripting.SlingScriptHelper;

import com.cqcomponents.api.cache.IComponentCache;
import com.cqcomponents.api.cache.IContentCache;

public class ExposeCacheContentTag extends AbstractComponentTag {

	@Override
	public void doTag() throws JspException, IOException {
		this.getPageContext().setAttribute("contentCache",
				getContentCache().getCache());
		this.getPageContext().setAttribute("componentCache",
				getComponentCache().getCache());
	}

	private IComponentCache getComponentCache() {
		SlingBindings bindings = (SlingBindings) getSlingHttpServletRequest()
				.getAttribute(SlingBindings.class.getName());
		SlingScriptHelper scriptHelper = bindings.getSling();
		return scriptHelper.getService(IComponentCache.class);
	}

	private IContentCache getContentCache() {
		SlingBindings bindings = (SlingBindings) getSlingHttpServletRequest()
				.getAttribute(SlingBindings.class.getName());
		SlingScriptHelper scriptHelper = bindings.getSling();
		return scriptHelper.getService(IContentCache.class);
	}
}